package com.ruoyi.web.controller.pc;


import com.ruoyi.business.domain.WmsInPlan;
import com.ruoyi.business.service.IWmsInPlanService;
import com.ruoyi.business.service.IWmsInWarehousService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 入库计划管理 Controller
 *
 * @author patton
 * @date 2023-07-16
 */
@RestController
@RequestMapping("/business/inPlan")
public class WmsInPlanController extends BaseController {

    @Autowired
    private IWmsInPlanService wmsInPlanService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IWmsInWarehousService wmsInWarehousService;

    /**
     * @Description: 列表
     * @author yunlong
     */
    @ApiOperation(value = "列表")
    @GetMapping("list")
    @CrossOrigin
    public TableDataInfo queryList(WmsInPlan info) {
        startPage();
        List<WmsInPlan> list = wmsInPlanService.selectWmsInPlanList(info);
        return getDataTable(list);
    }

    /**
     * 新增
     */
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WmsInPlan info)
    {
        WmsInPlan obj = wmsInPlanService.selectWmsInPlanBySn(info.getPlanSn());
        if(obj == null){
            SysUser sysUser = sysUserService.selectUserById(getUserId());
            info.setStatus(1);
            info.setCreateBy(sysUser.getNickName());
            return toAjax(wmsInPlanService.insertWmsInPlan(info));
        }else{
            return AjaxResult.error(1001,"入库计划单存在");
        }
    }

    /**
     * 修改
     */
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody WmsInPlan info)
    {
        WmsInPlan obj = wmsInPlanService.selectWmsInPlanBySn(info.getPlanSn());
        if(obj == null || (obj != null && obj.getId().equals(info.getId()))){
            SysUser sysUser = sysUserService.selectUserById(getUserId());
            info.setUpdateBy(sysUser.getNickName());
            return toAjax(wmsInPlanService.updateWmsInPlan(info));
        }else{
            return AjaxResult.error(1001,"入库计划单存在");
        }
    }

    /**
     * 取消
     */
    @Log(title = "取消", businessType = BusinessType.UPDATE)
    @PostMapping("/cancel")
    public AjaxResult cancel(@RequestBody WmsInPlan info)
    {
        info.setStatus(4);
        wmsInPlanService.updateWmsInPlan(info);
        //删除关联的入库单
        wmsInWarehousService.deleteWmsInWarehousByPlanId(info.getId());
        return AjaxResult.success();
    }

    /**
     * @Description: 详情接口
     * @author yunlong
     */
    @ApiOperation(value = "详情接口")
    @GetMapping("getDetail")
    @CrossOrigin
    public AjaxResult getDetail(WmsInPlan info) {
        List<WmsInPlan> list = wmsInPlanService.selectWmsInPlanList(info);
        return AjaxResult.success(list.get(0));
    }
}
